package com.chuanyang.driverh.mainhome.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.mainhome.WaybilDetailActivity;
import com.chuanyang.driverh.mainhome.WaybilDetailForCarPlanActivity;
import com.chuanyang.driverh.response2.ResultGoodsAllot;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.EventBusUtil;
import com.chuanyang.driverh.utils.eventbus.RefleshFragEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

import static com.chuanyang.driverh.R.id.tv_reassign;


public class CarPlanWaybilListAdapter extends BaseListAdapter<ResultGoodsAllot.DataBean> {//首页竞价大厅的adapter
    private Activity activity;
    private Dialog dialog;

    public CarPlanWaybilListAdapter(Activity activity) {
        super(activity);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.activity = activity;
    }

    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.home_item_car_plan_waybil_list, parent, false);
            holder = new ViewHolder();
            holder.iv_header = (ImageView) convertView.findViewById(R.id.iv_header);
            holder.tv_driver_name = (TextView) convertView.findViewById(R.id.tv_driver_name);
            holder.tv_car_band = (TextView) convertView.findViewById(R.id.tv_car_band);
            holder.tv_car_band_num = (TextView) convertView.findViewById(R.id.tv_car_band_num);
            holder.tv_waibil_no = (TextView) convertView.findViewById(R.id.tv_waibil_no);
            holder.tv_weight = (TextView) convertView.findViewById(R.id.tv_weight);
            holder.tv_reassign = (TextView) convertView.findViewById(tv_reassign);
            holder.iv_choose = (ImageView) convertView.findViewById(R.id.iv_choose);
            holder.tv_disagree = (TextView) convertView.findViewById(R.id.tv_disagree);
            holder.tv_delete = (TextView) convertView.findViewById(R.id.tv_delete);
            holder.ll_div = (LinearLayout) convertView.findViewById(R.id.ll_div);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ResultGoodsAllot.DataBean data = getItem(position);

        BitmapUtil.showImgForNetPath(data.getUserImage(), holder.iv_header);
        holder.tv_driver_name.setText(data.getUserName());
        if (data.getDriverType() == 2) {//2车 3船
            holder.tv_car_band.setText("牌");
        } else {
            holder.tv_car_band.setText("助");
        }
        holder.tv_car_band_num.setText(data.getPlateNumber());
        holder.tv_waibil_no.setText(data.getWaybillNo());
        holder.tv_weight.setText(StringUtil.getDouble(data.getDetailsWeight()) + "t");


        if (data.getApplyNoStatus() == 0) {//0 未申请放弃 1 申请放弃 2 队长拒绝
            holder.tv_reassign.setVisibility(View.GONE);
            holder.tv_disagree.setVisibility(View.GONE);
            holder.tv_delete.setVisibility(View.VISIBLE);
            holder.tv_delete.setText("撤回");
        } else if (data.getApplyNoStatus() == 1) {
            holder.tv_reassign.setVisibility(View.VISIBLE);
            holder.tv_reassign.setText("申请放弃");
            holder.tv_disagree.setVisibility(View.VISIBLE);
            holder.tv_delete.setVisibility(View.VISIBLE);
            holder.tv_delete.setText("同意");
        }else if (data.getApplyNoStatus() == 2) {
            holder.tv_reassign.setVisibility(View.GONE);
            holder.tv_disagree.setVisibility(View.GONE);
            holder.tv_delete.setVisibility(View.VISIBLE);
            holder.tv_delete.setText("撤回");
        }else if (data.getApplyNoStatus() == 3) {
            holder.tv_reassign.setVisibility(View.VISIBLE);
            holder.tv_reassign.setText("已撤回");
            holder.tv_disagree.setVisibility(View.GONE);
            holder.tv_delete.setVisibility(View.GONE);
        } else {
            holder.tv_reassign.setVisibility(View.GONE);
            holder.tv_disagree.setVisibility(View.GONE);
            holder.tv_delete.setVisibility(View.GONE);
        }



        if (data.getWaybillStatus()>=4){
            holder.tv_disagree.setVisibility(View.GONE);
            holder.tv_delete.setVisibility(View.GONE);
        }

        holder.tv_disagree.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                deleteWaybil(2, data.getWid());
            }
        });
        holder.tv_delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (data.getApplyNoStatus()==1){
                    dialog=DialogUtil.myDialog(activity, "删除", "取消", "确定", "同意司机放弃运单，将会删除该运单。", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (v.getId()==R.id.dialog_btn_right){
                                deleteWaybil(3, data.getWid());
                            }
                            dialog.cancel();
                        }
                    });
                }else {
                    dialog=DialogUtil.myDialog(activity, "撤回", "取消", "确定", "撤回后将删除该运单。", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (v.getId()==R.id.dialog_btn_right){
                                deleteWaybil(3, data.getWid());
                            }
                            dialog.cancel();
                        }
                    });

                }

            }
        });
        holder.ll_div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.getApplyNoStatus()==0){
                    Intent intent = new Intent(activity, WaybilDetailActivity.class);
                    intent.putExtra("waybilId", data.getWid() + "");
                    intent.putExtra("type", 2);
                    activity.startActivity(intent);
                }else {
                    Intent intent = new Intent(activity, WaybilDetailForCarPlanActivity.class);
                    intent.putExtra("waybilId", data.getWid() + "");
                    intent.putExtra("type", 2);
                    activity.startActivity(intent);
                }

            }
        });

        return convertView;
    }

    class ViewHolder {
        ImageView iv_header;
        TextView tv_driver_name;
        TextView tv_car_band;
        TextView tv_car_band_num;
        TextView tv_waibil_no;
        TextView tv_weight;
        ImageView iv_choose;
        TextView tv_reassign;
        TextView tv_disagree;
        TextView tv_delete;
        LinearLayout ll_div;

    }


    private void deleteWaybil(final int applyNoStatus, int waybilId) {// 	2 拒绝 3 同意（删除运单并更新订单的相关数据）
        //先删除，后重新分配。
        DialogUtil.showLoadingDialog(activity, "申请中...");
        Map<String, Object> params = new HashMap<String, Object>();
        ArrayList<String> waybillIds=new ArrayList<>();
        waybillIds.add(waybilId+"");
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("waybillIds", waybillIds);
        params.put("applyNoStatus", applyNoStatus);

        MyHttpUtil.deletWaybil(activity, params, new MyCallback<ResultGoodsAllot>(activity, new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activity, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(ResultGoodsAllot result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                   if (result.getMsg()!=null){
                       PrintUtil.toast(activity,result.getMsg());
                   }else {
                       PrintUtil.toast(activity,"操作成功");
                   }


                    EventBusUtil.postEvent(new RefleshFragEvent(5));
                } else {
                    PrintUtil.toast(activity, result.getMsg());
                }
            }
        });
    }
}
